<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\facade\Config;
use app\index\controller\User;
class Log extends User
{
    public function initialize()
    {
        parent::initialize();
        $this->token = session("login_token");
        if(!$this->token){
            $this->redirect("index/user/login");
        }
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        return view();
    }
    /**
     * 请求接口
     *获取所有应用类型
     * @return \think\Response
     */
    public function curl()
    {
        $data = array(
            'token' => session('login_token'),
            'terminal' => 'web',
            'starttime' => date("Y-m-d 00:00:00",time()),
            'endtime' => date("Y-m-d H:i:s", time()),
        );
        $data = json_encode($data);
        $url = request()->domain() . '/api/user/breakdown';
        $curlopt = array(
            CURLOPT_HTTPHEADER=>array ('Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen ( $data ) ),
            CURLOPT_TIMEOUT=>Config::get("site.api_timeout"),
        );
        $res = curl_post($url,$data,$curlopt);  
        $res = json_decode($res,true);
        if(isset($res['data']) && $res['code']==1){
            $data = array_slice($res['data'],0,10);
            $data = json_encode($data);
            return $data;
        }   
    }

    public function fenye()
    {
        $data = array(
            'token' => session('login_token'),
            'terminal' => 'web',
        );
        $data = json_encode($data);
        $url = request()->domain() . '/api/user/breakdown';
        $curlopt = array(
            CURLOPT_HTTPHEADER=>array ('Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen ( $data ) ),
            CURLOPT_TIMEOUT=>Config::get("site.api_timeout"),
        );
        $res = curl_post($url,$data,$curlopt);  
        $res = json_decode($res,true);
        $row = [];
        $lists = [];
        if($res['code'] == 1){
            foreach ($res['data'] as $key => $value) {
                if($key < 120){
                    $row['id'] = $key + 1;
                    $row['reporttime'] = $value['reporttime'];
                    $row['content'] = $value['content'];
                    array_push($lists,$row);
                } 
            }
        }else{
            $lists = [];
        }
        return $lists;
    }


}
